//
//  SYMultiLineTextView.m
//  NTBuildOC
//
//  Created by Marcus Yu on 2022/1/14.
//

#import "SYMultiLineTextView.h"

@import Masonry;

@interface SYMultiLineTextView ()<UITextViewDelegate>

@property (nonatomic, strong) UITextView *cardTextView;
@property (nonatomic, strong) MASConstraint *heightConstraint;

@end

@implementation SYMultiLineTextView

#pragma mark - ♻️life cycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

#pragma mark - 🔄overwrite

#pragma mark - 🚪public
- (void)setConfig:(SYMultiLineTextConfig *)config {
    _config = config;
    self.cardTextView.font = config.font;
    self.cardTextView.text = config.text;
    self.cardTextView.textAlignment = config.textAlignment;
    self.cardTextView.textColor = config.textColor;
}

#pragma mark - 🍐delegate
#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
    NSLog(@"-----%@",textView.text);
    
    NSLog(@"%d",[self textFitsWithTextView:textView]);

//    self.heightConstraint.mas_lessThanOrEqualTo(height);
}

#pragma mark - ☎️notification

#pragma mark - 🎬event response

#pragma mark - 🔒private

- (BOOL)textFitsWithTextView:(UITextView *)textView {
    CGFloat height = [self calculateTextViewHeight:textView];
    return height <= self.frame.size.height;
}

- (CGFloat)calculateTextViewHeight:(UITextView *)textView {
    CGSize constraintSize = CGSizeMake(textView.frame.size.width, MAXFLOAT);
    CGSize size = [textView sizeThatFits:constraintSize];
    return size.height;
}


#pragma mark - 🌲setupUI
- (void)setupUI {
    
    self.backgroundColor = [UIColor yellowColor];
    
    [self addSubview:self.cardTextView];
    
    __weak typeof(self) weakself = self;
    [self.cardTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.width.equalTo(self);
        weakself.heightConstraint = make.height.mas_lessThanOrEqualTo(self.frame.size.height);
    }];
}

#pragma mark - ✨getter and setter

-(UITextView *)cardTextView {
    if (_cardTextView == nil) {
        _cardTextView = [[UITextView alloc]init];
        _cardTextView.layer.borderWidth = 1.0;
        _cardTextView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _cardTextView.delegate = self;
        _cardTextView.scrollEnabled = NO;
        _cardTextView.backgroundColor = [UIColor lightGrayColor];
    }
    return _cardTextView;
}


@end
